package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.User;

import com.google.gson.Gson;
import com.mysql.jdbc.Connection;

import module.UserModule;

public class LoginController extends HttpServlet {
	

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		UserModule usermodule=new UserModule();
		Gson gson=new Gson();
		HttpSession session=request.getSession();
		response.setContentType("text/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		try {
			ArrayList list=usermodule.islogin(password, username);
			String userjson=gson.toJson(list);
			User user=(User)list.get(0);
			if(user.getLoginflag()==1){
				session.setAttribute("username",user.getUsername());
				session.setAttribute("userid",user.getUserid());
				
				Integer  test1=(Integer) session.getAttribute("userid");
				System.out.print(test1);

				response.sendRedirect("/CuitShopPlatform/index.html");
			}
			else
				response.sendRedirect("/CuitShopPlatform/login.html");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		out.flush();
		out.close();
	}

}
